/********************************************************************************
* @Author: Zhenzhou Liu
* @Date: 2024-11-17
* @Description: Cg贪心练习之最多活动选择
********************************************************************************/
#include <algorithm>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

struct Activity
{
    string name;
    int start;
    int end;
    Activity(string name, int start, int end): name(name), start(start), end(end){}
};

vector<string> maxActivities(vector<Activity>& activities, int startTime, int closeTime)
{
    sort(activities.begin(), activities.end(), [](const Activity& a, const Activity& b)
    {
        return a.end < b.end;
    });
    int lastEndTime = startTime;
    vector<string> res;
    for (Activity& activity : activities)
    {
        if (activity.start >= lastEndTime && activity.end <= closeTime)
        {
            res.push_back(activity.name);
            lastEndTime = activity.end;
        }
    }
    return res;
}

int main()
{
    vector<Activity> activities;
    string line;
    int startTime = 0, closeTime = 0;

    while (getline(cin, line) && !line.empty())
    {
        if (line[0] == 'C')
        {
            size_t minPos = line.find('-');
            startTime = stoi(line.substr(2, minPos - 2));
            closeTime = stoi(line.substr(minPos + 1));
            break;
        }
        size_t colonPos = line.find(':');
        size_t commaPos = line.find(',');
        if (colonPos != string::npos && commaPos != string::npos)
            activities.emplace_back(line.substr(0, colonPos), stoi(line.substr(colonPos + 1, commaPos - colonPos - 1)),
                                                                   stoi(line.substr(commaPos + 1)));
        else
        {
            cerr << "输入格式错误，请重新输入。" << endl;
            return 1;
        }
    }

    vector<string> activityNames = maxActivities(activities, startTime, closeTime);
    for (size_t i = 0; i < activityNames.size(); ++i)
    {
        if (i > 0)
            cout << ',';
        cout << activityNames[i];
    }
    return 0;

}